﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace Fjaspx.Common
{
    /// <summary>
    /// 获取URL地址的网页内容
    /// </summary>
    public class GetUrlText
    {
        /// <summary>
        /// Gets the text.
        /// </summary>
        /// <param name="url">地址 The URL.</param>
        /// <param name="Encoding">编码 The encoding.</param>
        /// <returns></returns>
        public static string GetText(string url, string Encoding)
        {
            WebResponse wr_result = null;
            StringBuilder txthtml = new StringBuilder();
            try
            {
                WebRequest wr_req = WebRequest.Create(url);
                wr_result = wr_req.GetResponse();
                Stream ReceiveStream = wr_result.GetResponseStream();
                Encoding encode = System.Text.Encoding.GetEncoding(Encoding);
                StreamReader sr = new StreamReader(ReceiveStream, encode);
                if (true)
                {
                    Char[] read = new Char[256];
                    int count = sr.Read(read, 0, 256);
                    while (count > 0)
                    {
                        String str = new String(read, 0, count);
                        txthtml.Append(str);
                        count = sr.Read(read, 0, 256);
                    }
                }
            }
            catch (Exception)
            {
                txthtml.Append("err");
            }
            finally
            {
                if (wr_result != null)
                {
                    wr_result.Close();
                }
            }
            return txthtml.ToString();
        }
    }
}
